package com.think.admin.config;

import com.think.common.enums.converter.IntegerEnumConverterFactory;
import com.think.common.enums.converter.StringEnumConverterFactory;
import com.think.common.utils.JacksonUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * <p>
 * Spring Boot 配置
 * </p>
 *
 * @author: xwj
 * @data: 2019-04-21
 */
@Configuration
public class WebMvcAutoConfig implements WebMvcConfigurer {

    /**
     * json 配置
     *
     * @param converters
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.forEach(JacksonUtils.wrapperObjectMapper());
    }

    /**
     * 参数枚举转换
     *
     * @param registry
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverterFactory(new IntegerEnumConverterFactory());
        registry.addConverterFactory(new StringEnumConverterFactory());
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("*");
    }
}
